package com.docking.heylo.web.conf;


import com.docking.heylo.web.interceptor.AuthorityInterceptor;
import com.docking.heylo.web.interceptor.GlobalizationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;


@Configuration
public class WebAppConfig implements WebMvcConfigurer {

    @Autowired
    private AuthorityInterceptor authorityInterceptor;

    @Autowired
    private GlobalizationInterceptor globalizationInterceptor;

    private List<String> excludePaths = new ArrayList<String>(){{
       add("/login");
    }};

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(globalizationInterceptor);
        registry.addInterceptor(authorityInterceptor).excludePathPatterns(excludePaths);
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }



}
